﻿//-----------------------------------------------------------------------
// <copyright file="Locator.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>

namespace ChatBox.UI
{
    using System;

    using ChatBox.Engine;
    using ChatBox.UI.Themes;

    using Microsoft.Practices.Unity;

    /// <summary>
    /// Class to hold the only singleton we'll need - the IOC container
    /// </summary>
    public static class Locator
    {
        /// <summary>
        /// The IOC container
        /// </summary>
        private static readonly IUnityContainer container = new UnityContainer();

        /// <summary>
        /// Initializes static members of the Locator class
        /// </summary>
        static Locator()
        {
            // register types from the chat engine
            UnityConfiguration.RegisterTypes(container);

            // Register Ui types - we only need one theme manager
            container.RegisterType<ThemeManager>(new ContainerControlledLifetimeManager());
        }

        /// <summary>
        /// Register an instance in the unity container
        /// </summary>
        /// <param name="instance">the instance to register</param>
        public static void RegisterInstance(object instance)
        {
            Type objectType = instance.GetType();
            container.RegisterInstance(objectType, instance);
        }

        /// <summary>
        /// Resolve an object from the IOC container
        /// </summary>
        /// <typeparam name="T">The type to resolve</typeparam>
        /// <returns>the new object</returns>
        public static T Resolve<T>()
        {
            return container.Resolve<T>();
        }
    }
}
